﻿///////////////////////////////////////////////////////////////////////////////////////////////
// System.Text.Parsing - © 2011 Sebastien Pellizzari
// Released under the MIT License (http://www.opensource.org/licenses/mit-license.php)
///////////////////////////////////////////////////////////////////////////////////////////////
using System.Collections;
using System.Collections.Generic;

namespace System.Text.Parsing
{
    /// <summary>Represents a collection of token definitions.</summary>
    public sealed class GrammarTokenCollection : IEnumerable<GrammarToken>
    {
        private readonly List<GrammarToken> _list;                    // Elements as list.
        private readonly Dictionary<string, GrammarToken> _elements;  // The collection of elements.

        //-----------------------------------------------------------------------
        //     Properties
        //-----------------------------------------------------------------------

        /// <summary>Gets the number of elements in the collection.</summary>
        public int Count { get { return _elements.Count; } }

        /// <summary>Gets the token definition object at specified index.</summary>
        /// <param name="index">The zero-based index of desired element.</param>
        public GrammarToken this[int index] { get { return _list[index]; } }

        /// <summary>Gets the token definition object with specified name, or a null reference if not found.</summary>
        /// <param name="name">The token name.</param>
        public GrammarToken this[string name]
        {
            get
            {
                GrammarToken token;
                if (_elements.TryGetValue(name, out token)) return token;
                return null;
            }
        }

        //-----------------------------------------------------------------------
        //     Constructors
        //-----------------------------------------------------------------------

        /// <summary>Initializes an instance of the <see cref="GrammarTokenCollection"/> class.</summary>
        public GrammarTokenCollection()
        {
            _list = new List<GrammarToken>();
            _elements = new Dictionary<string, GrammarToken>();
        }

        //-----------------------------------------------------------------------
        //     Methods
        //-----------------------------------------------------------------------

        /// <summary>Determines whether the collection contains a token definition with specified name.</summary>
        public bool Contains(string name) { return _elements.ContainsKey(name); }

        /// <summary>Adds an element to the collection.</summary>
        public void Add(GrammarToken token)
        {
            if (token == null) throw new ArgumentNullException("token");
            _list.Add(token);
            _elements.Add(token.Name.Text, token);
        }

        /// <summary>Returns an object that iterates through the collection.</summary>
        /// <returns>An object that implements the <see cref="IEnumerator{GrammarToken}"/> interface.</returns>
        public IEnumerator<GrammarToken> GetEnumerator() { return _list.GetEnumerator(); }

        //-----------------------------------------------------------------------
        //     Non Public
        //-----------------------------------------------------------------------

        // GetEnumerator method.
        IEnumerator IEnumerable.GetEnumerator() { return _list.GetEnumerator(); }
    }
}
